import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  // 状态
  const token = ref(localStorage.getItem('token') || '')
  const userInfo = ref(JSON.parse(localStorage.getItem('userInfo') || '{}'))

  // 设置token
  const setToken = (newToken) => {
    token.value = newToken
    localStorage.setItem('token', newToken)
  }

  // 设置用户信息
  const setUserInfo = (info) => {
    userInfo.value = info
    localStorage.setItem('userInfo', JSON.stringify(info))
  }

  // 清除用户状态
  const clearUserState = () => {
    token.value = ''
    userInfo.value = {} 
    localStorage.removeItem('token')
    localStorage.removeItem('userInfo')
  }

// 更新用户的 hasPayPassword 状态
const updateHasPayPassword = (hasPayPassword) => {
  userInfo.value.hasPayPassword = hasPayPassword
  localStorage.setItem('userInfo', JSON.stringify(userInfo.value))
}

  return {
    token,
    userInfo,
    setToken,
    setUserInfo,
    clearUserState,
    updateHasPayPassword
  }
}) 